﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Castle.ActiveRecord;
using NHibernate.Criterion;
using LIB;


public partial class admin_users_Editor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            litPos.Text = "<a href='/admin/users/default.aspx'>Module quản lý người dùng</a> <img src='/images/bul4.png'> ";
            int id = ConvertUtility.ToInt32(Request.QueryString["id"]);
            if (id > 0)
            {
                hidID.Value = id.ToString();
                initForm(true);
            }
            else
            {
                initForm(false);
            }
            
            fail.Visible = false;
           
        }
        catch (Exception ex) { }
    }
    protected void initForm(bool isEdit)
    {

        if (isEdit)
        {
            btnAdd.Visible = false;
            btnEdit.Visible = true;
            litPos.Text += "Sửa thông tin người dùng";
            fillData();
        }
        else
        {
            btnAdd.Visible = true;
            btnEdit.Visible = false;
            litPos.Text += "Thêm mới người dùng";
        }
    }

    protected void fillData()
    {
        try
        {
            Users_Info userInfo = Users_Info.Find(ConvertUtility.ToInt32(hidID.Value));
            imgAvatar.ImageUrl = App_Constant.PATH_UP_USERAVATAR + "/" + userInfo.avatar;
            hidAvatar.Value = userInfo.avatar;
            txtUsername.Text = userInfo.username;
        }
        catch (Exception ex) { }
    }
    protected Users_Info getUserInfo(Users_Info userInfo)
    {
        userInfo.avatar = fileUpAvatar.HasFile ? App_File.tienToFile() + "." + fileUpAvatar.FileName : hidAvatar.Value;
        userInfo.username = txtUsername.Text;
        return userInfo;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            if (Users_Info.Exists(Expression.Eq("username", txtUsername.Text.Trim())))
            {
                fail.Visible = true;
            }
            else
            {
                Users_Info userInfo = getUserInfo(new Users_Info());
                userInfo.password = ConvertUtility.DESEncrypt(App_Constant.FUNNY, txtPass.Text.Trim());
                userInfo.lastLogin = DateTime.Now;
                if (fileUpAvatar.HasFile)
                    App_File.uploadFileOnSerVer(fileUpAvatar, App_Constant.PATH_UP_USERAVATAR + "/", 150, 100);
                using (new SessionScope())
                    userInfo.Create();
                Response.Redirect("/admin/users/default.aspx?");
            }
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Users_Info userInfo = getUserInfo(Users_Info.Find(ConvertUtility.ToInt32(hidID.Value)));
            userInfo.password = ConvertUtility.DESEncrypt(App_Constant.FUNNY, txtPass.Text.Trim());
            if (fileUpAvatar.HasFile)
                App_File.uploadFileOnSerVer(fileUpAvatar, App_Constant.PATH_UP_USERAVATAR + "/", 150, 100);
            using (new SessionScope())
                userInfo.SaveAndFlush();
            Response.Redirect("/admin/users/default.aspx?");
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (hidID.Value != "")
            fillData();
        else
            ControlUtility.EmptyTextBoxes(this);

    }
}